<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Auction
 * @version 	: 1.1
 * COPYRIGHT    : 2010
 * ------------------------------------------------------
 *
 * Created on	:  Sep 13, 2010
 *
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );
RClassesImporter::cls('controllers.AbstractBackEndController');

/**
 * The controller for State application
 *
 */
class VideoController extends AbstractBackEndController {
	/**
	 *
	 * Main model
	 * @var VideoModel
	 */
	var $_model;

	/**
	 *
	 * Constructor
	 */
	function __construct() {
		parent::__construct();
		SysApplication::importModelCls('VideoModel');
		$this->_model = new VideoModel();
		$this->registerTask ( "new", "edit" );
		$this->registerTask ( "apply", "save" );
	}

	/**
	 * (non-PHPdoc)
	 * @see src/core/mvc/AbstractController::display()
	 */
	function display() {
		$this->setTitle (SysLanguage::_("Video Management") );
		$this->addToolbarButton ( "new" );
		$this->addToolbarButton ( "edit" );
		$this->addToolbarButton ( "delete" );
		$msg = SysRequest::getVar ( "msg" );
		RCoreImporter::cls( "html.TableControl" );
		RClassesImporter::cls("dbo.dtos.VideoDTO" );
		$table = new TableControl ( 'VideoDTO', NULL, $_REQUEST, 'VideoCtrl', '', '' );
		$nextExec = $this->getExecuteBy();
		$nextExec = ucfirst($nextExec);

		$table->setLinkRule("name",'javascript:edit'.$nextExec.'Item($key)');
		$table->setLinkRule("enabled", 'javascript:toggleEnabled($key)');

		$table->removeField('video_id');

		$tblHtml = $table->createHtmlTable(SystemSite::getSiteConfig()->list_limit);
		eval('echo "'.SysApplication::loadTemplate('back/listVideo').'";');
	}

	/*
	 * Edit VideoDTO
	 */
	function edit($dto = null) {
		$oModel = $this->_model;
		$cid = SysRequest::getVar ( "cid" );
		if (empty( $dto )) {
			if(is_array($cid)) {
				$cid = $cid[0];
			}
			$dto = $oModel->getVideoDetails($cid);
		}

		RCoreImporter::cls( "html.FormFieldsControl" );
		$formData = new FormFieldsControl ( $dto );

		$formData = $this->_getEditFormAndPrepareEditTask($dto, 'Video', true);
		$htmlContent = $formData->generateHtml();

		$msg = SysRequest::getVar('msg');
		eval('echo "'.SysApplication::loadTemplate('back/editVideo').'";');
	}

	/**
	 *
	 * Save VideoDTO
	 * @param boolean $isVideoEdit
	 */
	function save($isVideoEdit = false) {
		$model = $this->_model;
		$dto = new VideoDTO();
		if (SysRequest::getInt('id')) {
			$dto->resetToNull();
		}
		$dto->bind($_POST);
		if ($dto->checkKeyNotDefault()) {
			$dto->setEmptyNumberToZero();
		}
		if(empty($dto->enabled)) {
			$dto->enabled = 0;
		}
		RClassesImporter::cls('SugusHelper');
		if(SugusHelper::is_a_link($dto->link) === false) {
			$_REQUEST ["msg"] = $msg ? $msg : SysLanguage::_('INVALID LINK');
			return $this->edit($dto);
		}
		$result = $model->saveVideoDTO($dto);
		if ($dto->hasError())
		$msg = $dto->getAllErrorsMsg();

		if ($result === false || $this->getExecuteVal () == 'apply') {
			$_REQUEST ["msg"] = $msg ? $msg : SysLanguage::_ ( "ITEM_SAVE_SUCCESSFUL" );
			return $this->edit($dto);
		} else {
			$this->setRedirectToFunction('display', $msg ? $msg : SysLanguage::_ ( "ITEM_SAVE_SUCCESSFUL" ));
		}
	}

	/**
	 *
	 * Delete Videos
	 */
	function delete() {
		$model = $this->_model;
		$arrayIds = SysRequest::getVar ( "cid" );
		$model->delete($arrayIds);
		$this->setRedirectToFunction('display', SysLanguage::_ ( "ITEM_DELETE_SUCCESSFUL" ));
	}

	/**
	 *
	 * Toggle enabled status
	 */
	function toggleEnabled() {
		$id = SysRequest::getVar('cid');
		$model = &$this->_model;
		$model->toggle('enabled', $id[0]);
		$this->setRedirectToFunction('display');
	}

	/**
	 *
	 * Build Flash field info
	 * @param $inputName
	 */
	function &_getPicFileFiledInfo($inputName) {
		RCoreImporter::cls( "html.fieldInfo.FlashFileHtmlFieldInfo" );
		$field = new FlashFileHtmlFieldInfo($inputName,$linkRule);
		$field->setSupportedFilteType('.jpg,.jpeg,.bmp,.png,.gif');
		return $field;
	}
}